/*
 * Description: 聊天窗
 * Author: wangyifei
 * DateCreated: 2024-06-06 10:18
 */

import { useImperativeHandle, useState, type MutableRefObject } from 'react';
import { Modal } from 'antd';
import { useChatStore } from './store';
import BetterChat from './BetterChat';

export interface ChatRef {
	open: () => void;
	close: () => void;
}
type ChatProps = {
	charRef: MutableRefObject<ChatRef>;
	onClose?: () => void;
};
const JPBetterChat = (props: ChatProps) => {
	const { charRef, onClose } = props;
	const resetNewMsgNum = useChatStore((state) => state.resetNewMsgNum);
	const setChatStatus = useChatStore((state) => state.setChatStatus);

	const [open, setOpen] = useState(false);

	useImperativeHandle(charRef, () => ({
		open: () => {
			resetNewMsgNum();
			setChatStatus(true);
			setOpen(true);
		},
		close: () => setOpen(false)
	}));

	return (
		<Modal
			centered
			maskClosable={false}
			keyboard={false}
			footer={false}
			width={900}
			title="通讯窗口"
			open={open}
			onCancel={() => {
				setOpen(false);
				setChatStatus(false);
				onClose?.();
			}}
		>
			<BetterChat />
		</Modal>
	);
};

export { useChatStore };
export default JPBetterChat;
